package com.nariano.server.domain.engine.droolsimpl;


import java.util.logging.Logger;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

import com.nariano.server.domain.engine.RoundBasedEngine;
import com.nariano.server.publisher.ClientPublisherListener;
import com.nariano.server.publisher.ClientPublisherRegister;
import com.nariano.server.publisher.DefaultEventVisitor;
import com.nariano.server.publisher.Event;
import com.nariano.server.publisher.Visitor;
import com.nariano.server.publisher.events.AreaDiscovered;
import com.nariano.server.publisher.events.CityCreated;
import com.nariano.server.publisher.events.ConflictResolved;
import com.nariano.server.utils.aop.timelogging.LogExecTime;
import com.nariano.server.utils.dbtest.PopulationCounter;

public class NotDroolsEngineImpl extends RoundBasedEngine implements ClientPublisherListener {
	
	Logger logger = Logger.getLogger(NotDroolsEngineImpl.class.getCanonicalName());
	
	@Autowired
    @Qualifier(value="innerNotDrools")
	InnerNotDrools innerNotDrools;
	
	@Autowired
	@Qualifier(value="clientPublisher")
    private ClientPublisherRegister clientPublisherRegister;
	
	private int evolveCity=1; // default is evolve cities every time
	private int evolveCityCount=0;
	
	public void setEvolveCity(int evolveCity) {
		this.evolveCity = evolveCity;
	}

	@Override
	protected void doRun() {
		evolveCityCount++;
		if (evolveCityCount==evolveCity) {
			evolveCityCount=0;
			innerNotDrools.evolveCities();
			innerNotDrools.manageResources();
		} 
		
		innerNotDrools.resolveConflicts();
		innerNotDrools.moveWarriors();
		innerNotDrools.movePopulation();
	}
	
	@LogExecTime
	public void init() {
		clientPublisherRegister.add(this);
		innerNotDrools.init();
	}

	@Override
	public void onEvent(Event event) {
	
		event.visit(new DefaultEventVisitor( ) {

			@Override
			public void accept(CityCreated a) {
				innerNotDrools.createdCity(a.getArea());
			}

		});
	}


}
